# -*- coding: utf-8 -*-

#   This file is part of emesene.
#
#    Emesene is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    emesene is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with emesene; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

VERSION = '0.1'
import os
import gtk
# hack? no, you are wrong :P
path = os.path.split(os.path.split(__file__)[0])[0]
full_path = os.path.join(path, 'ContactList.py')
execfile(full_path)

import Plugin
import dialog
#import ContactList
import Menu
import abstract.MainMenu as MainMenu
import abstract.ContactMenu as ContactMenu
import abstract.GroupMenu as GroupMenu

class MainClass(Plugin.Plugin):
    '''Main plugin class'''
    description = 'Devel plugin to try a new Contact List'
    authors = { 'Luis Mariano Guerra' : 'luismarianoguerra gmail' }
    website = 'http://marianoguerra.com.ar'
    displayName = 'Contact List'
    name = 'CList'
    def __init__(self, controller, msn):
        '''Contructor'''
        
        Plugin.Plugin.__init__(self, controller, msn)
        
        self.description = 'Devel plugin to try a new Contact List'
        self.authors = { 'Luis Mariano Guerra' : 'luismarianoguerra gmail' }
        self.website = 'http://marianoguerra.com.ar'
        self.displayName = 'Contact List'
        self.name = 'CList'
        self.controller = controller
        self.contact_list = None
        self.window = None

    def start(self):
        '''start the plugin'''
        self.enabled = True
        self.contact_list = ContactList(self.controller.contacts, 
            self.controller.groups, dialog)
        #self.contact_list.order_by_status = True

        self.window = gtk.Window()
        self.window.set_default_size(300, 600)
        scroll = gtk.ScrolledWindow()
        vbox = gtk.VBox()

        self.menu = MainMenu.MainMenu(dialog, 
            self.controller.contacts, self.controller.groups, 
                self.contact_list)

        entry = gtk.Entry()
        entry.connect('changed', self._on_entry_changed)

        vbox.pack_start(Menu.build_menu_bar(self.menu), False)
        vbox.pack_start(entry, False)
        vbox.pack_start(scroll, True, True)

        self.contact_list.signal_connect('contact-selected', 
            self._on_contact_selected)
        self.contact_list.signal_connect('contact-menu-selected', 
            self._on_contact_menu_selected)
        self.contact_list.signal_connect('group-selected', 
            self._on_group_selected)
        self.contact_list.signal_connect('group-menu-selected', 
            self._on_group_menu_selected)

        scroll.add(self.contact_list)
        self.window.add(vbox)
        self.window.connect("delete-event", self._on_close)
        self.window.show_all()

    def _on_entry_changed(self, entry, *args):
        '''called when the text on entry changes'''
        self.contact_list.filter_text = entry.get_text()

    def _on_close(self, *args):
        '''callback called when the user close the window'''
        self.stop()
    
    def _on_contact_selected(self, contact_list, contact):
        '''callback for the contact-selected signal'''
        print 'contact selected: ', contact.display_name
        
    def _on_group_selected(self, contact_list, group):
        '''callback for the group-selected signal'''
        print 'group selected: ', group.name
    
    def _on_contact_menu_selected(self, contact_list, contact):
        '''callback for the contact-menu-selected signal'''
        contact_menu = ContactMenu.ContactMenu(contact, 
            self.controller.contacts, dialog)
        menu = Menu.build_pop_up(contact_menu, 0, 0)
        contact_menu.block_item.enabled = not contact.blocked
        contact_menu.unblock_item.enabled = contact.blocked
        menu.popup(None, None, None, 0, 0)

    def _on_group_menu_selected(self, contact_list, group):
        '''callback for the group-menu-selected signal'''
        group_menu = GroupMenu.GroupMenu(group, 
            self.controller.groups, self.controller.contacts, dialog)
        menu = Menu.build_pop_up(group_menu, 0, 0)
        menu.popup(None, None, None, 0, 0)

    def stop(self):    
        '''stop the plugin'''
        self.window.hide()
        self.enabled = False
        self.contact_list = None
        self.window = None

    def check(self):
        '''Check Plugin'''
        return (True, 'Ok')

